package com.wgl.order.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wgl.model.order.OrderInfo;
import com.wgl.model.vo.msm.MsmVo;
import com.wgl.order.service.OrderService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName RocketMqReceiver.java
 * @Description TODO
 * @createTime 2022年02月16日 14:46:00
 */
@Component
public class RocketMqReceiver {

    @Value("${rocketmq.nameserver}")
    private String nameserver;

    @Resource
    private OrderService orderService;

    @PostConstruct
    public void send(){
        DefaultMQPushConsumer yygh_xf = new DefaultMQPushConsumer("yygh_task");
        //连接NameServer
        yygh_xf.setNamesrvAddr(nameserver);
        try {
            yygh_xf.subscribe("yygh", "task");
            yygh_xf.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                    msgs.forEach(messageExt -> {
                        orderService.patientTips();
                        System.out.println("task消费！！");
                    });
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
            yygh_xf.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }

    }
}
